package org.edu.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;

import org.edu.model.Project;
import org.edu.service.ProjectService;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.SelectableDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class ProjectController extends ListDataModel<Project> implements Serializable, SelectableDataModel<Project> {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient ProjectService projectService;
	private Project project;
	private List<Project> projectList;
	private List<Project> filterProject;
	
	public ProjectController(){
		project = new Project();
	}
	
	@PostConstruct
	public void init(){
		projectList = projectService.projects();
	}
	
	public void preparedAddProject() {
		project = new Project();
	}

	public String save() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			projectService.save(project);
			facesContext.addMessage(null , new FacesMessage("Save Project is Succesful" , project.getProject_code()));
			init();
			project = new Project();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Error!" ,"Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void preparedUpdateProject(Long project_id) throws Exception {
		project = projectService.findById(project_id);
	}

	public String update() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			projectService.update(project);
			facesContext.addMessage(null , new FacesMessage("Update Project is Succesful" , project.getProject_code()));
			init();
			project = new Project();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Error!" ,"Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void preparedDeleteProject(Long project_id) throws Exception {
		project = projectService.findById(project_id);
	}

	public String delete() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			projectService.remove(project);
			facesContext.addMessage(null , new FacesMessage("Delete Project is Succesful" , project.getProject_code()));
			init();
			project = new Project();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Error!" ,"Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Project Selected", ((Project) event.getObject()).getProject_code());  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
	}

	@Override
	public Object getRowKey(Project object) {
		return project.getProject_code();
	}

	@Override
	public Project getRowData(String rowKey) {
		for(Project project : projectList){
			if(project.getProject_code().equals(rowKey))
				return project;
		}
		return null;
	}

	public Project getProject() {
		if(project == null){
			project = new Project();
		}
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public List<Project> getProjectList() {
		return projectList;
	}

	public List<Project> getFilterProject() {
		return filterProject;
	}

	public void setFilterProject(List<Project> filterProject) {
		this.filterProject = filterProject;
	}

}
